package com.etcn.common.utils;

import java.io.InputStream;
import java.util.Map;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.etcn.common.config.ErrorConfig;
import com.etcn.common.exception.BusinessException;
import com.etcn.common.utils.http.HttpSend;

/**
 * 公共方法
 * 
 * @author guozc
 *
 */
public class PubFun {

    private static final Logger LOG = LoggerFactory.getLogger(PubFun.class);

    private static ErrorConfig errorConfig;

    static {
        errorConfig = SpringUtil.getBean("errorConfig", ErrorConfig.class);
    }

    /**
     * 返回业务错误信息给客户端
     * 
     * @param errorCode 错误编码
     */
    public static void renderError(String errorCode) throws Exception {
        throw new BusinessException(errorConfig.getConfig("zhcn.error." + errorCode));
    }

    /**
     * 返回业务错误信息给客户端
     * 
     * @param errorMsg 错误信息
     */
    public static void renderErrorMsg(String errorMsg) throws Exception {
        throw new BusinessException(errorMsg);
    }

    /**
     * 调用第三方接口
     * 
     * @param url 接口地址
     * @param returnType 返回值类型
     * @return
     * @throws Exception
     */
    public static <T> T requestInterface(String url, Class<T> returnType) throws Exception {
        HttpSend http = new HttpSend();
        http.sendGet(url);
        int httpStatus = http.httpStatus();
        if (httpStatus != 200) {
            return null;
        }
        String retMsg = http.returnMsg();
        if (StringUtil.isNotEmpty(retMsg)) {
            retMsg = retMsg.replaceAll("\"null\"", "\"\"");
        }
        JsonHandler json = JsonHandler.buildNoNull();
        T ret = null;
        try {
            ret = json.toObject(retMsg, returnType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 调用第三方接口(POST)
     * 
     * @param url 接口地址
     * @param params 请求参数
     * @param returnType 返回值类型
     * @return
     * @throws Exception
     */
    public static <T> T requestPostInterface(String url, Map<String, Object> params,String urlParams, Class<T> returnType)
        throws Exception {
        HttpSend http = new HttpSend();
        if(params != null) {
            http.sendJsonPost(url, params);
        }else {
            http.sendNoJsonPost(url, urlParams);
        }
        int httpStatus = http.httpStatus();
        if (httpStatus != 200) {
            return null;
        }
        String retMsg = http.returnMsg();
        if (StringUtil.isNotEmpty(retMsg)) {
            retMsg = retMsg.replaceAll("\"null\"", "\"\"");
        }
        JsonHandler json = JsonHandler.buildNoNull();
        T ret = null;
        try {
            ret = json.toObject(retMsg, returnType);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 将classpath下json文件转为JSONObject
     * @param jsonFile json文件
     */
    public static JSONObject getJson(String jsonFile) throws Exception {
        String json = "{}";
        InputStream inputStream = null;
        try {
            ClassPathResource resource = new ClassPathResource(jsonFile);
            inputStream = resource.getInputStream();
            json = IOUtils.toString(inputStream, "UTF-8");
        } catch (Exception e) {
            LOG.error("读取JSON文件异常", e);
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
        return JSONObject.parseObject(json);
    }

}
